package com.key.utils;

import com.key.common.dto.AssetsAddDTO;
import com.key.common.dto.AssetsFindDTO;
import com.key.common.dto.StocktakingAssetDTO;
import com.key.common.dto.StocktakingFormDTO;
import com.key.common.vo.AssetsVO;
import com.key.common.vo.StocktakingFormDetailsVO;
import com.key.entity.Assets;
import com.key.entity.StocktakingForm;
import com.key.entity.StocktakingResult;
import com.key.mapper.AssetModelMapper;
import com.key.mapper.CAreaMapper;
import com.key.mapper.CompanyMapper;
import com.key.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.List;

/**
 * 类型转换工具包
 * @author wei
 * @create 2023-02-21 10:11
 */
public class EntityConversion {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private CAreaMapper cAreaMapper;

    @Autowired
    private AssetModelMapper assetModelMapper;

    public static StocktakingResult StocktakingAssetDTOToStocktakingResult(StocktakingAssetDTO stocktakingAssetDTO) {
        StocktakingResult stocktakingResult = new StocktakingResult();

        stocktakingResult.setSfNo(stocktakingAssetDTO.getSfNo());

        return stocktakingResult;
    }

    public Assets AssetsAddDTOToAssets(AssetsAddDTO assetsAddDTO){

        Assets assets = new Assets();

        assets.setAssetsId(assetsAddDTO.getAssetsId());
        assets.setAssetsPhoto(assetsAddDTO.getAssetsPhoto());
        assets.setAssetsCode(assetsAddDTO.getAssetsCode());
        assets.setAssetsName(assetsAddDTO.getAssetsName());
        assets.setAssetsSerialNumber(assetsAddDTO.getAssetsSerialNumber());
        assets.setAssetsMoney(assetsAddDTO.getAssetsMoney());

        assets.setAssetsEmployeeId(employeeMapper.selectEmpIdByEmpName(assetsAddDTO.getAssetsEmployeeName()));
        assets.setAssetsStorageLocation(assetsAddDTO.getAssetsStorageLocation());

        assets.setAssetsManagerId(employeeMapper.selectEmpIdByEmpName(assetsAddDTO.getAssetsManagerName()));

        assets.setAssetsAdminId(companyMapper.selectCmpIdByCmpName(assetsAddDTO.getAssetsAdminName()));
        assets.setAssetsPurchaseDate(assetsAddDTO.getAssetsPurchaseDate());
        assets.setAssetsServiceLife(assetsAddDTO.getAssetsServiceLife());
        assets.setAssetsSource(assetsAddDTO.getAssetsSource());

        assets.setAssetsCreaterId(employeeMapper.selectEmpIdByEmpName(assetsAddDTO.getAssetsCreaterName()));
        assets.setAssetsReceiptNo(assetsAddDTO.getAssetsReceiptNo());
        assets.setAssetsRemarks(assetsAddDTO.getAssetsRemarks());
        assets.setAssetsEnclosure(assetsAddDTO.getAssetsEnclosure());

        assets.setAssetsRegionId(cAreaMapper.selectCAreaIdByCAreaName(assetsAddDTO.getAssetsRegionName()));

        assets.setAssetsModelId(assetModelMapper.selectModelIdByModelName(assetsAddDTO.getAssetsModelName()));
        assets.setSupplier(assetsAddDTO.getSupplier());
        assets.setContacts(assetsAddDTO.getContacts());
        assets.setContactInfo(assetsAddDTO.getContactInfo());
        assets.setPersonInCharge(assetsAddDTO.getPersonInCharge());
        assets.setExpirationTime(assetsAddDTO.getExpirationTime());
        assets.setExplain(assetsAddDTO.getExplain());

        return assets;
    }

    public Assets AssetsAddDTOToAssets(AssetsAddDTO assetsAddDTO, Timestamp timestamp){
        Assets assets = AssetsAddDTOToAssets(assetsAddDTO);

        assets.setAssetsCreateTime(timestamp);

        return assets;
    }

    public static AssetsFindDTO StocktakingFormDTOToAssetsFindDTO(StocktakingFormDTO stocktakingFormDTO){

        AssetsFindDTO assetsFindDTO = new AssetsFindDTO();

        assetsFindDTO.setAssetsPurchaseDate(null);
        assetsFindDTO.setDepartmentName(stocktakingFormDTO.getDepartmentName());
        assetsFindDTO.setModelName(stocktakingFormDTO.getAssetsModelName());
        assetsFindDTO.setAssetsRegionName(stocktakingFormDTO.getAssetsRegionName());
        assetsFindDTO.setAssetsState(stocktakingFormDTO.getAssetsState());

        return assetsFindDTO;
    }

    public static StocktakingForm StocktakingFormDTOToStocktakingForm(StocktakingFormDTO stocktakingFormDTO){

        StocktakingForm stocktakingForm = new StocktakingForm();

        stocktakingForm.setSfName(stocktakingFormDTO.getSfName());
        stocktakingForm.setSfRemark(stocktakingFormDTO.getSfRemark());
        stocktakingForm.setSfCreater(stocktakingFormDTO.getSfCreater());
        stocktakingForm.setSfIsAll(stocktakingFormDTO.getSfIsAll());
        stocktakingForm.setSfIsPlate(stocktakingFormDTO.getSfIsPlate());
        stocktakingForm.setSfIsManual(stocktakingFormDTO.getSfIsManual());
        stocktakingForm.setSfIsUpload(stocktakingFormDTO.getSfIsUpload());
        stocktakingForm.setSfIsContainCallout(stocktakingFormDTO.getSfIsContainCallout());

        return stocktakingForm;
    }

    public static StocktakingForm StocktakingFormDTOToStocktakingForm(StocktakingFormDTO stocktakingFormDTO, Timestamp timestamp){

        StocktakingForm stocktakingForm = StocktakingFormDTOToStocktakingForm(stocktakingFormDTO);


        stocktakingForm.setSfCreateTime(timestamp);


        return stocktakingForm;
    }

    public static StocktakingResult AssetsVOToStocktakingResult(AssetsVO assetsVO, Timestamp timestamp) {
        StocktakingResult stocktakingResult = new StocktakingResult();

        stocktakingResult.setAssetsId(assetsVO.getAssetsId());
        stocktakingResult.setSrCreateTime(timestamp);
        stocktakingResult.setOriginalAssetsStorageLocation(assetsVO.getAssetsStorageLocation());
        stocktakingResult.setOriginalAssetsRegion(assetsVO.getAssetsRegionName());
        stocktakingResult.setOriginalEmployeeName(assetsVO.getEmployeeName());
        stocktakingResult.setOriginalDepartmentName(assetsVO.getDepartmentName());
        stocktakingResult.setOriginalCompanyName(assetsVO.getCompanyName());

        return stocktakingResult;
    }

    public static StocktakingFormDetailsVO stocktakingFormToStocktakingFormDetailsVO(List<String> employeeNameList, StocktakingForm stocktakingForm) {
        StocktakingFormDetailsVO stocktakingFormDetailsVO = new StocktakingFormDetailsVO();

        stocktakingFormDetailsVO.setSfId(stocktakingForm.getSfId());
        stocktakingFormDetailsVO.setSfNo(stocktakingForm.getSfNo());
        stocktakingFormDetailsVO.setSfName(stocktakingForm.getSfName());
        stocktakingFormDetailsVO.setSfRemark(stocktakingForm.getSfRemark());
        stocktakingFormDetailsVO.setSfCreater(stocktakingForm.getSfCreater());
        stocktakingFormDetailsVO.setSfState(stocktakingForm.getSfState());
        stocktakingFormDetailsVO.setSfCreateTime(stocktakingForm.getSfCreateTime());
        stocktakingFormDetailsVO.setSfIsAll(stocktakingForm.getSfIsAll());
        stocktakingFormDetailsVO.setSfIsPlate(stocktakingForm.getSfIsPlate());
        stocktakingFormDetailsVO.setSfIsManual(stocktakingForm.getSfIsManual());
        stocktakingFormDetailsVO.setSfIsUpload(stocktakingForm.getSfIsUpload());
        stocktakingFormDetailsVO.setSfIsContainCallout(stocktakingForm.getSfIsContainCallout());
        stocktakingFormDetailsVO.setAssignEmpsName(employeeNameList);

        return stocktakingFormDetailsVO;
    }

    public Assets StocktakingAssetDTOToAssets(StocktakingAssetDTO stocktakingAssetDTO, Timestamp timestamp) {
        Assets assets = new Assets();

        assets.setAssetsId(stocktakingAssetDTO.getAssetsId());
        assets.setAssetsCode(stocktakingAssetDTO.getAssetsCode());
        assets.setAssetsName(stocktakingAssetDTO.getAssetsName());
        assets.setAssetsModelId(assetModelMapper.selectModelIdByModelName(stocktakingAssetDTO.getModelName()));
        assets.setAssetsAdminId(companyMapper.selectCmpIdByCmpName(stocktakingAssetDTO.getAssetsAdminName()));
        assets.setAssetsEmployeeId(employeeMapper.selectEmpIdByEmpName(stocktakingAssetDTO.getAssetsEmployeeName()));
        assets.setAssetsRegionId(cAreaMapper.selectCAreaIdByCAreaName(stocktakingAssetDTO.getAssetsRegionName()));
        assets.setAssetsStorageLocation(stocktakingAssetDTO.getAssetsStorageLocation());
        assets.setAssetsRemarks(stocktakingAssetDTO.getAssetsRemarks());
        assets.setAssetsCreateTime(timestamp);

        return assets;
    }
}
